package gui;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.ProductType;
import model.Stack;
import service.LocationNotFoundException;
import service.Service;

/**
 * 
 * @author Ceslovas Lopan
 * 
 */
public class AddStack extends JFrame {
	private final Controller controller = new Controller();
	private JTextField txtTrays;
	private JComboBox cbTypes;
	private JButton btnAdd, btnCancel;
	private MainFrame owner;

	public AddStack(MainFrame owner) {
		this.owner = owner;
		setSize(300, 134);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setLocation(100, 100);
		setResizable(false);
		setTitle("Add stack");
		getContentPane().setLayout(new GridLayout(4, 0, 0, 4));

		JPanel panel3 = new JPanel();
		getContentPane().add(panel3);

		JPanel panel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setVgap(0);
		flowLayout.setAlignment(FlowLayout.LEFT);
		getContentPane().add(panel);

		JLabel lblTrays = new JLabel("Trays:");
		panel.add(lblTrays);

		txtTrays = new JTextField();
		panel.add(txtTrays);
		txtTrays.setColumns(10);

		JPanel panel1 = new JPanel();
		FlowLayout flowLayout1 = (FlowLayout) panel1.getLayout();
		flowLayout1.setVgap(0);
		flowLayout1.setAlignment(FlowLayout.LEFT);
		getContentPane().add(panel1);

		JLabel lblProductType = new JLabel("Product type:");
		panel1.add(lblProductType);

		Object[] productTypes = Service.getInstance().getProductTypes()
				.toArray();
		cbTypes = new JComboBox(productTypes);
		panel1.add(cbTypes);

		JPanel panel2 = new JPanel();
		FlowLayout flowLayout2 = (FlowLayout) panel2.getLayout();
		flowLayout2.setVgap(0);
		flowLayout2.setAlignment(FlowLayout.RIGHT);
		getContentPane().add(panel2);

		btnAdd = new JButton("Add");
		panel2.add(btnAdd);
		btnAdd.addActionListener(controller);

		btnCancel = new JButton("Cancel");
		panel2.add(btnCancel);
		btnCancel.addActionListener(controller);

		JLabel lblCurrentLocation = new JLabel("");
		panel3.add(lblCurrentLocation);

		String location = null;
		try {
			location = Service.getInstance().findFreeLocation().toString();
		} catch (LocationNotFoundException e) {
			location = "There is no location available";
		}
		lblCurrentLocation.setText("Location: " + location);
	}

	private class Controller implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == btnCancel) {
				AddStack.this.dispose();
			} else if (e.getSource() == btnAdd) {
				ProductType ptype = (ProductType) cbTypes.getSelectedItem();

				try {
					int trays = Integer.parseInt(txtTrays.getText());

					try {
						Stack stack = Service.getInstance().addStack(ptype,
								trays);

						if (stack != null) {
							AddStack.this.owner.getStockPanel().updateView();
							AddStack.this.dispose();
						}

						else {
							JOptionPane.showMessageDialog(AddStack.this,
									"Could not create stack", "Error",
									JOptionPane.ERROR_MESSAGE);
						}

					} catch (LocationNotFoundException e1) {
						JOptionPane.showMessageDialog(AddStack.this,
								"Could not find location", "Error",
								JOptionPane.ERROR_MESSAGE);
					}
				} catch (NumberFormatException ex) {
					JOptionPane.showMessageDialog(AddStack.this,
							"Trays should be number", "Error",
							JOptionPane.ERROR_MESSAGE);
				}
			}

		}

	}
}
